package com.cx.mall.cart.service.impl;

import com.cx.mall.cart.service.ICartService;
import com.cx.mall.proto.Cart;
import com.cx.mall.proto.RequestParam;
import com.cx.mall.proto.ResponseResult;
import com.cx.mall.proto.RpcSimpleGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@GrpcService
public class GrpcCartServiceImpl extends RpcSimpleGrpc.RpcSimpleImplBase {
    @Autowired
    private ICartService cartService;
    @Override
    public void deleteCart(RequestParam request, StreamObserver<ResponseResult> responseObserver) {
        // 获取参数
        List<Cart> cartsList = request.getCartsList();
        // 获取购物车Id集合
        List<String> ids = cartsList.stream().map(Cart::getId).collect(Collectors.toList());
        // 删除购物车
        cartService.removeAllById(ids);
// 构建返回值
        ResponseResult response = ResponseResult.newBuilder()
                .setCode(2000)
                .build();
        responseObserver.onNext(response);
        // RPC调用完成
        responseObserver.onCompleted();
    }
}